/*
' Copyright (c) 2012-2013  Kyriakos Lesgidis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using System.Data;
using net.diadrasis.CameraSlideshow.Components;


namespace net.diadrasis.CameraSlideshow
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// 
    /// Typically your settings control would be used to manage settings for your module.
    /// There are two types of settings, ModuleSettings, and TabModuleSettings.
    /// 
    /// ModuleSettings apply to all "copies" of a module on a site, no matter which page the module is on. 
    /// 
    /// TabModuleSettings apply only to the current module on the current page, if you copy that module to
    /// another page the settings are not transferred.
    /// 
    /// If you happen to save both TabModuleSettings and ModuleSettings, TabModuleSettings overrides ModuleSettings.
    /// 
    /// Below we have some examples of how to access these settings but you will need to uncomment to use.
    /// 
    /// Because the control inherits from CameraSlideshowSettingsBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : CameraSlideshowSettingsBase
    {

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    DataTable dt = new DataTable();
                    dt.Load(cObjectController.getObject(ModuleId));

                    if (dt.Rows.Count > 0)
                    {
                        drpAlignment.SelectedValue = dt.Rows[0]["objectAlignment"].ToString();                                                
                        drpAutoAdvance.SelectedValue = dt.Rows[0]["objectAutoAdvance"].ToString();
                        txtTransitions.Text = dt.Rows[0]["objectTransitions"].ToString();
                        txtHeight.Text = dt.Rows[0]["objectHeight"].ToString();
                        drpHover.SelectedValue = dt.Rows[0]["objectHover"].ToString();
                        drpLoader.SelectedValue = dt.Rows[0]["objectLoader"].ToString();
                        txtLoaderColor.Text = dt.Rows[0]["objectLoaderColor"].ToString();
                        txtLoaderBgColor.Text = dt.Rows[0]["objectLoaderBgColor"].ToString();
                        txtLoaderOpacity.Text = dt.Rows[0]["objectLoaderOpacity"].ToString();
                        drpNavigation.SelectedValue = dt.Rows[0]["objectNavigation"].ToString();
                        drpNavigationHover.SelectedValue = dt.Rows[0]["objectNavigationHover"].ToString();
                        drpPlayPause.SelectedValue = dt.Rows[0]["objectPlayPause"].ToString();
                        drpPauseOnClick.SelectedValue = dt.Rows[0]["objectPauseOnClick"].ToString();
                        txtPieDiameter.Text = dt.Rows[0]["objectPieDiameter"].ToString();
                        drpPiePosition.Text = dt.Rows[0]["objectPiePosition"].ToString();
                        drpPortrait.SelectedValue = dt.Rows[0]["objectPortrait"].ToString();
                        drpThumbnails.SelectedValue = dt.Rows[0]["objectThumbnails"].ToString();
                        drpPagination.SelectedValue = dt.Rows[0]["objectPagination"].ToString();
                        txtTime.Text = dt.Rows[0]["objectTime"].ToString();
                        txtTransPeriod.Text = dt.Rows[0]["objectTransPeriod"].ToString();                       
                    }
                    else
                    {
                        //error
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings()
        {
            try
            {
                //ModuleController modules = new ModuleController();
                cObject o = new cObject();
                o.objectAlignment = drpAlignment.SelectedValue;
                o.objectAutoAdvance = (drpAutoAdvance.SelectedValue == "True");
                o.objectTransitions = txtTransitions.Text;
                o.objectHeight = txtHeight.Text;
                o.objectHover = (drpHover.SelectedValue == "True");
                o.objectLoader = drpLoader.SelectedValue;
                o.objectLoaderColor = txtLoaderColor.Text;
                o.objectLoaderBgColor = txtLoaderBgColor.Text;
                o.objectLoaderOpacity = (float) Convert.ToDouble(txtLoaderOpacity.Text);
                o.objectNavigation = (drpNavigation.SelectedValue == "True");
                o.objectNavigationHover = (drpNavigationHover.SelectedValue == "True");
                o.objectPlayPause = (drpPlayPause.SelectedValue == "True");
                o.objectPauseOnClick = (drpPauseOnClick.SelectedValue == "True");
                o.objectPieDiameter = Convert.ToInt32(txtPieDiameter.Text);
                o.objectPiePosition = drpPiePosition.Text;
                o.objectPortrait = (drpPortrait.SelectedValue == "True");
                o.objectThumbnails = (drpThumbnails.SelectedValue == "True");
                o.objectPagination = (drpPagination.SelectedValue == "True");
                o.objectTime = Convert.ToInt32(txtTime.Text);
                o.objectTransPeriod = Convert.ToInt32(txtTransPeriod.Text);                

                cObjectController.updateObject(o, ModuleId);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }

}

